package com.lsp.server.login.controller;

import com.lsp.server.common.Apiresponse;
import com.lsp.server.common.ResultData;
import com.lsp.server.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName LoginController
 * @Description 登录注册
 * @Author Mr Hao
 * @Date 2021/11/17 17:31
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService loginService;

    /**
     * 登录
     * @param name
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Apiresponse login(String name , String password){
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)){
            return Apiresponse.Error(400,"用户或密码不能为空");
        }

        return Apiresponse.success(loginService.login(name,password));
    }

    /**
     * 找回密码
     *
     * @param request
     * @return
     */
    @PostMapping( "/forgetPass")
    public ResultData forgetPass(HttpServletRequest request, String name, String password) {
        if(StringUtils.isEmpty(name)|| StringUtils.isEmpty(password)){
            return ResultData.needParam();
        }
        loginService.updatePass(request, name,password);
        return ResultData.success("");
    }
}
